// 获取起点到最短线路的垂点
function getVerticalPoint(point, ans) {
	var VerticalPoints = [];
	var x = point.x,
		y = point.y;
	for(let i = 0; i < ans.length; i++) {
		var k = ans[i].ana.k,
			b = ans[i].ana.b;
		var verticalPoint;
		if(ans[i].ana.k == null) {
			verticalPoint = VerticalPoint.createNew(ans[i].ana.p1.x, y, ans[i].ana);
		} else {
			verticalPoint = VerticalPoint.createNew((y * k + x * 1 - b * k) / (k * k + 1), (y * k * k + x * k + b * 1) / (k * k + 1), ans[i].ana);
		}
		VerticalPoints.push(verticalPoint);
	}
	return VerticalPoints;
}

// 获取最短距离的路线
function getMinWay(point) {
	var dList = [];
	var result = [];
	for(let i = 0; i < analyticals.length; i++) {
		var a = {
			"d": getDistinct(point, analyticals[i]),
			"ana": analyticals[i]
		};
		dList.push(a);
	}
	dList.sort(function(a, b) {
		return a.d - b.d;
	});
	for(let i = 0; i < dList.length; i++) {
		if(dList[0].d == dList[i].d) {
			result.push(dList[i]);
		}
	}
	return result;
}

// 计算点到线路的距离
function getDistinct(p, ana) {
	var result = MAX_NUM;
	var x = p.x,
		y = p.y;
	var A = ana.k,
		B = -1,
		C = ana.b;
	var jValue = false
	if(ana.k == null) {
		jValue = y < Math.max(ana.p1.y, ana.p2.y) && y > Math.min(ana.p1.y, ana.p2.y);
		if(jValue) {
			result = Math.abs(ana.p1.x - p.x);
		}
	} else {
		if(ana.k == 0) {
			jValue = x < Math.max(ana.p1.x, ana.p2.x) && x > Math.min(ana.p1.x, ana.p2.x);
		} else {
			jValue = x < Math.max(ana.p1.x, ana.p2.x) && x > Math.min(ana.p1.x, ana.p2.x) && y < Math.max(ana.p1.y, ana.p2.y) && y > Math.min(ana.p1.y, ana.p2.y);
		}
		if(jValue) {
			result = Math.abs(A * x + B * y + C * 1) / Math.sqrt(A * A + B * B);
		}
	}
	result = result.toFixed(2) * 1;
	return result;
}
// 根据垂点获取拐点
function getInflectionPoint(verticalPoints) {
	var result = [];
	for(let i = 0; i < verticalPoints.length; i++) {
		var pre = Point.createNew(verticalPoints[i].x, verticalPoints[i].y);
		var p1 = InflectionPoint.createNew(verticalPoints[i].ana.p1.x, verticalPoints[i].ana.p1.y, Math.sqrt(Math.pow(verticalPoints[i].ana.p1.x - verticalPoints[i].x, 2) + Math.pow(verticalPoints[i].ana.p1.y - verticalPoints[i].y, 2)), pre);
		var p2 = InflectionPoint.createNew(verticalPoints[i].ana.p2.x, verticalPoints[i].ana.p2.y, Math.sqrt(Math.pow(verticalPoints[i].ana.p2.x - verticalPoints[i].x, 2) + Math.pow(verticalPoints[i].ana.p2.y - verticalPoints[i].y, 2)), pre);
		result.push(p1);
		result.push(p2);
	}
	result = pointMoveRepeat(result);
	return result;
}

// 点去重
function pointMoveRepeat(points) {
	for(var i = 0; i < points.length; i++) {
		for(var j = 1; j < points.length - 1; j++) {
			if(i < points.length) {
				if(checkPoints(points[i], points[j])) {
					points.splice(j, 1);
				}
			}
		}
	}
	return points;
}

// 判断两个点是否会相等
function checkPoints(p1, p2) {
	var result = false;
	if(p1.x == p2.x && p1.y == p2.y && p1.d == p2.d) {
		result = true;
	}
	return result;
}